﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRIP;
using GRIP.Gestures;
using TUIO;
using System.Threading;

namespace Tester
{
    public partial class Tester : Form
    {
        KinectAdapter kinect;
        GestureMessenger messenger;
        Dictionary<long, string> GestureInfo = new Dictionary<long, string>();
        
        public Tester()
        {
            InitializeComponent();
            KinectSettings settings = new KinectSettings();
            kinect = new KinectAdapter(settings);
            kinect.AllowInferredContacts = true;
            Recognizer.SetAdapter(kinect);
            messenger = new GestureMessenger("localhost");
            SetListeners();
        }

        public void AddInfo(Gesture g, string newInfo)
        {
            //infoBox.Text += newInfo + Environment.NewLine;
            GestureInfo[g.ID] = newInfo;
            RefreshInfo();
        }

        public void UpdateInfo(Gesture g, string newInfo)
        {
            GestureInfo[g.ID] = newInfo;
            RefreshInfo();
            
            //infoBox.Text += newInfo + Environment.NewLine;
        }

        public void RemoveInfo(Gesture g, string newInfo)
        {
            GestureInfo.Remove(g.ID);
            RefreshInfo();
            //infoBox.Text += newInfo + Environment.NewLine;
        }

        public void RefreshInfo()
        {
            string newInfo = "";
            foreach (string info in GestureInfo.Values)
                newInfo += info + Environment.NewLine;
            infoBox.Text = newInfo;
        }

        public void BasicAdd(BasicGesture g, int frameNumber)
        {
            AddInfo(g, "Frame " + frameNumber + ": " + g.ToString() + " " + g.ID + " " + g.Cardinality + " added");
        }
        public void BasicUpdate(BasicGesture g, int frameNumber)
        {
            UpdateInfo(g, "Frame " + frameNumber + ": " + g.ToString() + " " + g.ID + " " + g.Cardinality + ": " + g.CurrentPosition);
        }
        public void BasicRemove(BasicGesture g, int frameNumber)
        {
            RemoveInfo(g, "Frame " + frameNumber + ": " + g.ToString() + " " + g.ID + " " + g.Cardinality +" died");
        }

        public void ZoomAdd(ZoomGesture g, int frameNumber)
        {
            AddInfo(g, "Frame " + frameNumber + ": " + g.ToString() + " " + g.ID + " added");
        }
        public void ZoomUpdate(ZoomGesture g, int frameNumber)
        {
            UpdateInfo(g, "Frame " + frameNumber + ": " + g.ToString() + " " + g.ID + ": " + g.Magnitude);
        }
        public void ZoomRemove(ZoomGesture g, int frameNumber)
        {
            RemoveInfo(g, "Frame " + frameNumber + ": " + g.ToString() + " " + g.ID + " died");
        }
        
        public void SetListeners()
        {
            SlideA.Added += new EventHandler<GestureEventArgs<SlideA>>(
                (object sender, GestureEventArgs<SlideA> e) => BasicAdd(e.Gesture, e.FrameNumber)
            );
            SlideA.Changed += new EventHandler<GestureEventArgs<SlideA>>(
                (object sender, GestureEventArgs<SlideA> e) => BasicUpdate(e.Gesture, e.FrameNumber)
            );
            SlideA.Removed += new EventHandler<GestureEventArgs<SlideA>>(
                (object sender, GestureEventArgs<SlideA> e) => BasicRemove(e.Gesture, e.FrameNumber)
            );

            SlideB.Added += new EventHandler<GestureEventArgs<SlideB>>(
                (object sender, GestureEventArgs<SlideB> e) => BasicAdd(e.Gesture, e.FrameNumber)
            );
            SlideB.Changed += new EventHandler<GestureEventArgs<SlideB>>(
                (object sender, GestureEventArgs<SlideB> e) => BasicUpdate(e.Gesture, e.FrameNumber)
            );
            SlideB.Removed += new EventHandler<GestureEventArgs<SlideB>>(
                (object sender, GestureEventArgs<SlideB> e) => BasicRemove(e.Gesture, e.FrameNumber)
            );

            SlideC.Added += new EventHandler<GestureEventArgs<SlideC>>(
                (object sender, GestureEventArgs<SlideC> e) => BasicAdd(e.Gesture, e.FrameNumber)
            );
            SlideC.Changed += new EventHandler<GestureEventArgs<SlideC>>(
                (object sender, GestureEventArgs<SlideC> e) => BasicUpdate(e.Gesture, e.FrameNumber)
            );
            SlideC.Removed += new EventHandler<GestureEventArgs<SlideC>>(
                (object sender, GestureEventArgs<SlideC> e) => BasicRemove(e.Gesture, e.FrameNumber)
            );

            DragA.Added += new EventHandler<GestureEventArgs<DragA>>(
                (object sender, GestureEventArgs<DragA> e) => BasicAdd(e.Gesture, e.FrameNumber)
            );
            DragA.Changed += new EventHandler<GestureEventArgs<DragA>>(
                (object sender, GestureEventArgs<DragA> e) => BasicUpdate(e.Gesture, e.FrameNumber)
            );
            DragA.Removed += new EventHandler<GestureEventArgs<DragA>>(
                (object sender, GestureEventArgs<DragA> e) => BasicRemove(e.Gesture, e.FrameNumber)
            );

            DragB.Added += new EventHandler<GestureEventArgs<DragB>>(
                (object sender, GestureEventArgs<DragB> e) => BasicAdd(e.Gesture, e.FrameNumber)
            );
            DragB.Changed += new EventHandler<GestureEventArgs<DragB>>(
                (object sender, GestureEventArgs<DragB> e) => BasicUpdate(e.Gesture, e.FrameNumber)
            );
            DragB.Removed += new EventHandler<GestureEventArgs<DragB>>(
                (object sender, GestureEventArgs<DragB> e) => BasicRemove(e.Gesture, e.FrameNumber)
            );

            DragC.Added += new EventHandler<GestureEventArgs<DragC>>(
                (object sender, GestureEventArgs<DragC> e) => BasicAdd(e.Gesture, e.FrameNumber)
            );
            DragC.Changed += new EventHandler<GestureEventArgs<DragC>>(
                (object sender, GestureEventArgs<DragC> e) => BasicUpdate(e.Gesture, e.FrameNumber)
            );
            DragC.Removed += new EventHandler<GestureEventArgs<DragC>>(
                (object sender, GestureEventArgs<DragC> e) => BasicRemove(e.Gesture, e.FrameNumber)
            );

            ZoomGesture.Added += new EventHandler<GestureEventArgs<ZoomGesture>>(
                (object sender, GestureEventArgs<ZoomGesture> e) => ZoomAdd(e.Gesture, e.FrameNumber)
            );
            ZoomGesture.Changed += new EventHandler<GestureEventArgs<ZoomGesture>>(
                (object sender, GestureEventArgs<ZoomGesture> e) => ZoomUpdate(e.Gesture, e.FrameNumber)
            );
            ZoomGesture.Removed += new EventHandler<GestureEventArgs<ZoomGesture>>(
                (object sender, GestureEventArgs<ZoomGesture> e) => ZoomRemove(e.Gesture, e.FrameNumber)
            );
        }

        private void Tester_FormClosing(object sender, FormClosingEventArgs e)
        {
            messenger.Close();
            kinect.Shutdown();
        }

    }
}
